package com.itheima.web.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * 替换hTTpServlet，根据请求的最后一段路径来进行方法分发
 */

public class BaseServlet extends HttpServlet {
    //根据请求的最后一段路径来进行方法分发

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求路径
        String uri = req.getRequestURI();
        //2.获取最后一段路径，其实就是方法名
        int i = uri.lastIndexOf("/");

        String methododName = uri.substring(i + 1);
        System.out.println(methododName);


        //3.执行方法

        //3.1获取对应方法的字节码对象class
        Class<? extends BaseServlet> cls = this.getClass();


        //3.2获取方法method对象
        try {
            Method method = cls.getMethod(methododName, HttpServletRequest.class, HttpServletResponse.class);
            //3.3执行方法
            method.invoke(this,req,resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }
}
